package rpg;

public class TimerTask extends java.util.TimerTask 
{
	RPGCanvas rpgCanvas;
	int type;
	int s;
	int n;
	int j;
	public TimerTask(RPGCanvas g,int i)
	{
		this.rpgCanvas=g;
		this.type=i;
		this.s=1;
		this.n=0;
	}
	public void run()
	{
		if(RPGCanvas.running)
		{			
			switch(type)
			{
        case -2:
        rpgCanvas.isWeather=true;
        if(n%1000==0)
        {
        	s=rpgCanvas.getRandom(3);
        	rpgCanvas.snow=new int[30][30];
        	for(int i=0;i<rpgCanvas.snow.length;i++)
        	for(int j=0;j<rpgCanvas.snow[0].length;j++)
        	rpgCanvas.snow[i][j]=30000;
        }
        if(n==60000)
        n=0;
        n++;
        rpgCanvas.repaint();
        break;
				case 13:
				s++;
				if(s==12)
				rpgCanvas.isChange=false;
				rpgCanvas.repaint();
				break;
				case 10:
				switch(s)
				{
					case 1:
					rpgCanvas.spic0=rpgCanvas.spic[5];
					rpgCanvas.sx=36;
					rpgCanvas.sy=200;
					rpgCanvas.repaint();
					break;
					case 2:
					rpgCanvas.sy=168;
					rpgCanvas.repaint();
					break;
					case 3:
					rpgCanvas.sy=200;
					rpgCanvas.repaint();
					break;
					case 4:
					rpgCanvas.sy=168;
					rpgCanvas.life+=(rpgCanvas.level*rpgCanvas.level+10*rpgCanvas.level+60)*15*rpgCanvas.skillArray[9]/100;
					if(rpgCanvas.life>rpgCanvas.maxlife)
					rpgCanvas.life=rpgCanvas.maxlife;
					rpgCanvas.SPI[0].life=rpgCanvas.life;
					rpgCanvas.repaint();
					break;
					case 5:
					rpgCanvas.spic0=null;
					rpgCanvas.attackNumber=4;
					rpgCanvas.repaint();
					rpgCanvas.cancelAttackTask();
					break;
					default:
					break;
				}
				s++;
				break;				 
				case 11:
				switch(s)
				{
					case 1:
					rpgCanvas.spic0=rpgCanvas.spic[4];
					rpgCanvas.sx=36;
					rpgCanvas.sy=200;
					rpgCanvas.repaint();
					break;
					case 2:
					rpgCanvas.sy=168;
					rpgCanvas.repaint();
					break;
					case 3:
					rpgCanvas.sy=200;
					rpgCanvas.repaint();
					break;
					case 4:
					rpgCanvas.sy=168;
					rpgCanvas.energy+=rpgCanvas.maxenergy*20*rpgCanvas.skillArray[10]/100;
					if(rpgCanvas.energy>rpgCanvas.maxenergy)
					rpgCanvas.energy=rpgCanvas.maxenergy;
					rpgCanvas.repaint();
					break;
					case 5:
					rpgCanvas.spic0=null;
					rpgCanvas.attackNumber=4;
					rpgCanvas.repaint();
					rpgCanvas.cancelAttackTask();
					break;
					default:
					break;
				}
				s++;
				break;			
				case 8:
				if(rpgCanvas.isFighting&&!rpgCanvas.isAttMenu&&rpgCanvas.attackTask==null)
				{
					int i;
					for(i=4;i<8;i++)
					{
						if(rpgCanvas.SPI[i]!=null)
						break;
					}
					if(i==8)
					{
						if(!rpgCanvas.isWin)
						{
							if(!rpgCanvas.isProMenu)
							{
								rpgCanvas.isWin=true;
								rpgCanvas.isUpdated=true;
								rpgCanvas.isFighting=false;
							
								if(MenuCanvas.isAudioOn)
								{
									MenuCanvas.se.stopSound(rpgCanvas.attPlayer);
									rpgCanvas.mapPlayer=MenuCanvas.se.createPlayer("/title.mid",1000);
									
									MenuCanvas.se.playSound(rpgCanvas.mapPlayer);
								}							
								for(int k=1;k<4;k++)
								rpgCanvas.SPI[k]=null;	
								if(rpgCanvas.tran)
								{
									rpgCanvas.tran=!rpgCanvas.tran;
									rpgCanvas.maxlife=rpgCanvas.level*rpgCanvas.level+10*rpgCanvas.level+60;
									rpgCanvas.life=rpgCanvas.life-rpgCanvas.life*10*rpgCanvas.skillArray[8]/(100+10*rpgCanvas.skillArray[8])>rpgCanvas.maxlife?rpgCanvas.maxlife:(rpgCanvas.life-rpgCanvas.life*10*rpgCanvas.skillArray[8]/(100+10*rpgCanvas.skillArray[8]));
									rpgCanvas.basicPower=5*rpgCanvas.level+2;
									rpgCanvas.totalPower=rpgCanvas.basicPower+rpgCanvas.addPower+rpgCanvas.addPdP*rpgCanvas.basicPower/100;
								}							
								rpgCanvas.repaint();
							}
						}
					}
					else
					{
						if(rpgCanvas.life==0)
						{
							if(!rpgCanvas.isProMenu)
							{
								rpgCanvas.isDie=true;
								rpgCanvas.isFighting=false;
								if(MenuCanvas.isAudioOn)
								{
									MenuCanvas.se.stopSound(rpgCanvas.attPlayer);
									rpgCanvas.mapPlayer=MenuCanvas.se.createPlayer("/title.mid",1000);
									MenuCanvas.se.playSound(rpgCanvas.mapPlayer);
								}												
								for(int k=1;k<8;k++)
								rpgCanvas.SPI[k]=null;
								if(rpgCanvas.tran)
								{
									rpgCanvas.tran=!rpgCanvas.tran;
									rpgCanvas.maxlife=rpgCanvas.level*rpgCanvas.level+10*rpgCanvas.level+60;
									rpgCanvas.life=rpgCanvas.life-rpgCanvas.life*10*rpgCanvas.skillArray[8]/(100+10*rpgCanvas.skillArray[8])>rpgCanvas.maxlife?rpgCanvas.maxlife:(rpgCanvas.life-rpgCanvas.life*10*rpgCanvas.skillArray[8]/(100+10*rpgCanvas.skillArray[8]));
									rpgCanvas.basicPower=5*rpgCanvas.level+2;
									rpgCanvas.totalPower=rpgCanvas.basicPower+rpgCanvas.basicPower*rpgCanvas.addPdP/100+rpgCanvas.addPower;
								}
								rpgCanvas.repaint();
							}
						}
						else
						if(rpgCanvas.attackNumber<3&&rpgCanvas.attackNumber>0)
						{
							if(rpgCanvas.SPI[rpgCanvas.attackNumber]!=null)
							rpgCanvas.setAttack(rpgCanvas.attackNumber+20);
							else
							rpgCanvas.attackNumber=rpgCanvas.attackNumber==2?1:0;
						}
						else
						if(rpgCanvas.attackNumber==0)
						{
							rpgCanvas.isAttMenu=true;
							if(rpgCanvas.isHiding)
							{
								rpgCanvas.count++;
								if(rpgCanvas.count==1+rpgCanvas.skillArray[6])
								rpgCanvas.isHiding=!rpgCanvas.isHiding;
							}
							else
							if(rpgCanvas.tran)
							{
								rpgCanvas.count++;
								if(rpgCanvas.count>rpgCanvas.skillArray[8])
								{
									rpgCanvas.tran=!rpgCanvas.tran;
									rpgCanvas.maxlife=rpgCanvas.level*rpgCanvas.level+10*rpgCanvas.level+60;
									rpgCanvas.life=rpgCanvas.life*80/100>rpgCanvas.maxlife?rpgCanvas.maxlife:rpgCanvas.life*80/100;
									rpgCanvas.SPI[0].life=rpgCanvas.life;
									rpgCanvas.SPI[0].basicPower=5*rpgCanvas.level+2;
									rpgCanvas.basicPower=rpgCanvas.SPI[0].basicPower;
								}
							}
							rpgCanvas.repaint();
						}
						else
						{
							while(rpgCanvas.attackNumber<8)
							{
								if(rpgCanvas.SPI[rpgCanvas.attackNumber]!=null)
								{
									rpgCanvas.setAttack(12);
									break;
								}
								else
								rpgCanvas.attackNumber++;
							}
							if(rpgCanvas.attackNumber>7)
							rpgCanvas.attackNumber=2;	
						}
					}
				}
				break;
				case 9:
				if(s==1)
				{
					for(int i=3;i>=0;i--)
					{
						if(rpgCanvas.SPI[i]!=null)
						{
							if((i==0&&!rpgCanvas.isHiding)||i!=0)
							{
								rpgCanvas.SPI[j].x=rpgCanvas.SPI[i].x+16;
								rpgCanvas.SPI[j].y=rpgCanvas.SPI[i].y/32*32;
								rpgCanvas.SPI[j].p++;
								if(i!=0&&rpgCanvas.SPI[j].level==15)
								rpgCanvas.SPI[i].life-=2*rpgCanvas.SPI[j].basicPower;
								else
								rpgCanvas.SPI[i].life-=rpgCanvas.SPI[j].basicPower;
								if(rpgCanvas.SPI[i].life<0)
								rpgCanvas.SPI[i].life=0;
								rpgCanvas.life=rpgCanvas.SPI[0].life;
								if(i==3&&rpgCanvas.SPI[j].level!=15)
								rpgCanvas.SPI[j].life-=rpgCanvas.SPI[j].basicPower*10*rpgCanvas.skillArray[5]/100;
								if(rpgCanvas.SPI[j].life<0)
								rpgCanvas.SPI[j].life=0;
								rpgCanvas.repaint();
								n=i;
								break;
							}
							else
							s=4;
						}
					}
				}
				else
				if(s==2)
				{
					rpgCanvas.SPI[n].x-=5;
					if(n==0)
					rpgCanvas.SPI[0].p=141;
					rpgCanvas.repaint();
				}
				else
				if(s==3)
				{				
					rpgCanvas.SPI[n].x+=5;
					rpgCanvas.SPI[j].x=166;
					rpgCanvas.SPI[j].y=(j-4)*32+((rpgCanvas.SPI[j].level-1)/3==1?15:0)+64+108;
					rpgCanvas.SPI[j].p--;		
					if(n==0)
					{
						if(!rpgCanvas.tran)
						rpgCanvas.SPI[0].p=130;
						else
						rpgCanvas.SPI[0].p=150;
					}
					if(rpgCanvas.SPI[n].life==0)
					rpgCanvas.SPI[n]=null;
					if(rpgCanvas.SPI[j].life==0)
					{
						if(rpgCanvas.SPI[0].life>0)
						{
							rpgCanvas.sn+=100*rpgCanvas.SPI[j].level*rpgCanvas.SPI[j].level/rpgCanvas.level;
							rpgCanvas.nn+=4*rpgCanvas.SPI[j].level*rpgCanvas.SPI[j].level;
						}
						rpgCanvas.SPI[j]=null;				
					}
					rpgCanvas.repaint();
				}
				else
				{
					rpgCanvas.attackNumber++;
					if(rpgCanvas.attackNumber>7)
					rpgCanvas.attackNumber=2;				
					rpgCanvas.repaint();
					rpgCanvas.cancelAttackTask();				
				}
				s++;
				break;			
				case 7:
				rpgCanvas.move();
				break;
				case 4:
				switch(s)
				{
					case 1:
					for(int i=0;i<4;i++)
					{
						if(rpgCanvas.SPI[i+4]!=null)
						{
							rpgCanvas.sx=rpgCanvas.SPI[i+4].x;
							rpgCanvas.sy=rpgCanvas.SPI[i+4].y-16;
							rpgCanvas.spic0=rpgCanvas.spic[3];
							rpgCanvas.repaint();
							n=i;
							break;
						}
					}
					break;
					case 2:
					rpgCanvas.sy+=8;
					rpgCanvas.repaint();
					break;
					case 3:
					rpgCanvas.sy+=8;
					rpgCanvas.spic0=rpgCanvas.spic[0];
					rpgCanvas.repaint();
					break;
					case 4:
					rpgCanvas.sy-=2;
					rpgCanvas.repaint();
					break;
					case 5:
					rpgCanvas.sy+=2;
					if(rpgCanvas.SPI[n+4].level!=15)
					rpgCanvas.SPI[n+4].life-=(5*rpgCanvas.skillArray[0]*rpgCanvas.skillArray[0]+15)*(100+rpgCanvas.stealB)/100;
					if(rpgCanvas.SPI[n+4].life<0)
					rpgCanvas.SPI[n+4].life=0;				
					rpgCanvas.repaint();
					break;
					case 6:
					rpgCanvas.spic0=null;
					if(rpgCanvas.SPI[n+4].life==0)
					{
						rpgCanvas.sn+=100*rpgCanvas.SPI[n+4].level*rpgCanvas.SPI[n+4].level/rpgCanvas.level;
						rpgCanvas.nn+=4*rpgCanvas.SPI[n+4].level*rpgCanvas.SPI[n+4].level;					
						rpgCanvas.SPI[n+4]=null;
					}
					rpgCanvas.repaint();
					break;
					case 7:	
					rpgCanvas.attackNumber=4;
					rpgCanvas.repaint();
					rpgCanvas.cancelAttackTask();
					break;
					default:
					break;							
				}
				s++;
				break;
				case 5:
				s++;
				rpgCanvas.repaint();
				break;
				case 6:
				s++;
				rpgCanvas.repaint();
				break;
				default:
				if(s==1)
				{
					for(int i=0;i<4;i++)
					{
						if(rpgCanvas.SPI[i+4]!=null)
						{
							rpgCanvas.SPI[type].x=rpgCanvas.SPI[i+4].x-16;
							rpgCanvas.SPI[type].y=rpgCanvas.SPI[i+4].y;
							rpgCanvas.SPI[type].p++;
							if(type==0)
							{
								if(rpgCanvas.SPI[i+4].life<rpgCanvas.totalPower)
								rpgCanvas.life+=rpgCanvas.SPI[i+4].life*rpgCanvas.stealB/100;
								else
								rpgCanvas.life+=rpgCanvas.totalPower*rpgCanvas.stealB/100;
								if(rpgCanvas.life>rpgCanvas.maxlife)
								rpgCanvas.life=rpgCanvas.maxlife;
								rpgCanvas.SPI[0].life=rpgCanvas.life;
							}
							rpgCanvas.SPI[i+4].life-=rpgCanvas.SPI[type].basicPower;			
							if(rpgCanvas.SPI[i+4].life<0)
							rpgCanvas.SPI[i+4].life=0;
							rpgCanvas.repaint();
							n=i;
							break;
						}
					}
				}
				else
				if(s==2)
				{
					rpgCanvas.SPI[n+4].x+=5;
					rpgCanvas.repaint();
				}
				else
				if(s==3)
				{
					rpgCanvas.SPI[n+4].x-=5;
					rpgCanvas.SPI[type].x=36;
					rpgCanvas.SPI[type].y=96-32*type+(type==1?16:0)+64+64;
					rpgCanvas.SPI[type].p--;				
					if(rpgCanvas.SPI[n+4].life==0)
					{
						rpgCanvas.sn+=100*rpgCanvas.SPI[n+4].level*rpgCanvas.SPI[n+4].level/rpgCanvas.level;
						rpgCanvas.nn+=4*rpgCanvas.SPI[n+4].level*rpgCanvas.SPI[n+4].level;
						rpgCanvas.SPI[n+4]=null;
					}
					rpgCanvas.repaint();			
				}
				else
				{
					rpgCanvas.attackNumber--;
					if(rpgCanvas.attackNumber<0)
					rpgCanvas.attackNumber=4;
					rpgCanvas.repaint();
					rpgCanvas.cancelAttackTask();
				}
				s++;
				break;
			}
		}
	}
}